# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: inkyu


class OrderContainer(object):
  def __init__(self):
    self._working_orders = {}
    self._orders_by_exchange_order_id = {}

  @property
  def working_orders(self):
    return self._working_orders

  @property
  def working_orders_items(self):
    return self._working_orders.items()

  def add(self, order, *, working=True):
    assert order.order_id, order
    if working:
      order_id = order.order_id
      self._working_orders[order_id] = order
    if order.internal.exchange_order_id:
      exchange_order_id = order.internal.exchange_order_id
      self._orders_by_exchange_order_id[exchange_order_id] = order

  def pop_by_exchange_order_id(self, exchange_order_id):
    return self._orders_by_exchange_order_id.pop(exchange_order_id, None)

  def assign_exchange_order_id(self, order, exchange_order_id):
    assert exchange_order_id not in self._orders_by_exchange_order_id, exchange_order_id
    order.internal.exchange_order_id = exchange_order_id
    self._orders_by_exchange_order_id[exchange_order_id] = order

  def by_exchange_order_id(self, exchange_order_id):
    return self._orders_by_exchange_order_id.get(exchange_order_id, None)

  def get_working_order(self, order_id):
    return self._working_orders.get(order_id, None)

  def remove_from_working_order(self, order_id):
    return self._working_orders.pop(order_id, None)

  def unacked_order_id_list(self):
    return [order.order_id for order in self._working_orders.values() if not order.accepted]
